<?php

namespace app\admin\controller;


use app\admin\model\Partno;
use cmf\controller\AdminBaseController;
use think\facade\Db;


class PartnoController extends AdminBaseController
{
                /**
              *列表
              * @adminMenu(
              *     'name'   => '工序管理',
              *     'parent' => 'user/AdminAsset/index',
              *     'display'=> true,
              *     'hasView'=> false,
              *     'order'  => 10000,
              *     'icon'   => '',
              *     'remark' => '',
              *     'param'  => ''
              * )
            */
    public function index(){
        //搜索部分
//        $key_status = input('get.key_status/d');
        $grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');
        $this->assign('grade_id_arr', $grade_id_arr);
        $lists = Partno::order('partno_id desc');
        $lists = $lists->where('delete_time', 0);
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }

        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);

        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(),'domain'=>request()->domain().'/upload/']);
    }

    public function add(){
        $grade_id_arr = Db::query('select id,g_name from cmf_grade where delete_time =0');
        $this->assign('grade_id_arr', $grade_id_arr);
        return $this->fetch('',['domain'=>request()->domain().'/upload/']);
    }

    public function addPost(){
        $params = array_map(function($v){
            return is_array($v)?implode(',',$v):$v;
        },input('post.'));

        if(!$params){
            $this->error('非法参数');
        }
        $params['add_time'] = time();
        $params['update_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
//        var_dump($params);
//        exit();
        Partno::create($params);

        $this->success('操作成功');
    }

    public function edit(){
        $id = input('get.partno_id/d');

        if(!is_numeric($id)){
            $this->error('非法参数');
        }

        $res = Partno::where('partno_id',$id)->find();

        $grade_id_arr = Db::query('select id,g_name from cmf_grade where delete_time =0');
        $this->assign('grade_id_arr', $grade_id_arr);
        return $this->fetch('',['vo'=>$res,'domain'=>request()->domain().'/upload/']);
    }

    public function editPost(){
        $params = array_map(function($v){
                    return is_array($v)?implode(',',$v):$v;
                    },input('post.'));

        if(!$params || empty($params['partno_id'])){
            $this->error('非法参数');
        }
//        $params['add_time'] = time();
        $params['update_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
//        unset($params['partno_id']);


        Db::name('partno')
//            ->where('partno_id',$params['partno_id'])
            ->update($params);
//        var_dump(Db::name('partno')->getLastSql());

        $this->success('操作成功','index');
    }

    public function del(){
        $id = input('get.partno_id/d');

        if(!(is_numeric($id) && $id>0)){
            $this->error('非法参数');
        }

        Partno::where('partno_id',$id)->delete();

        $this->success('删除成功');
    }

    public function field(){
         $id = input('get.partno_id/d');
         $field = input('get.field');

            if(!(is_numeric($id) && $id>0 && $field)){
            $this->error('非法参数');
        }

        $info = Partno::field($field)->where('partno_id',$id)->find();

        $this->success('ok','',$info[$field]);
    }

    /**上传excel导入零件号
     * @return void
     */
    public function importexcel(){

//http://172.20.2.2/upload/admin/20221021/e23a2bc833856b2faac4a22b256befc3.xlsx
            //$file为本地文件地址
            $file = "upload/admin/20221021/e23a2bc833856b2faac4a22b256befc3.xlsx";
            $data = inpor($file);

//        $excel_model = array('导入识别码', '业主类型', '业主名称', '性别', '电子邮箱', '证件类型', '证件号码', '主要联系手机', '备用联系电话', '出生日期', '职业', '籍贯', 'QQ', '微信号', '兴趣爱好', '现住地址', '业主备注');
//        $excel_title = $data[1];//excel头部标题部分
//        if ($excel_title !== $excel_model) {
//            $this->ajaxReturn(['status' => -1, 'msg' => 'excel数据格式错误,请下载并参照excel模板']);
////            $this->error('excel数据格式错误,请下载并参照excel模板', U('Property/house/owner_import'));
//        }

            var_dump($data);//得到了Excel中的数据，可进行插入数据库等操作

    }
}